/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.web;

import com.google.inject.Inject;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.controller.LifecycleStage;
import org.xptracker.domain.Domain;
import org.xptracker.domain.Story;
import org.xptracker.domain.StoryDescriptionSnapshot;
import org.xptracker.domain.WikiContent;
import org.xptracker.persistence.PersistenceManager;
import org.xptracker.util.Diff;
import org.xptracker.util.SideBySideRenderer;

public class StoryDiffActionBean extends StandardActionBean {
  @Inject
  private Domain domain;

  private String storyId;
  private String snapshotId;

  private StoryDescriptionSnapshot snapshot;
  private Story story;
  private SideBySideRenderer<String> descriptionRenderer;
  private SideBySideRenderer<String> summaryRenderer;

  public String getStoryId() {
    return storyId;
  }

  public void setStoryId(String storyId) {
    this.storyId = storyId;
  }

  public String getSnapshotId() {
    return snapshotId;
  }

  public void setSnapshotId(String snapshotId) {
    this.snapshotId = snapshotId;
  }

  public StoryDescriptionSnapshot getSnapshot() {
    return snapshot;
  }

  public void setSnapshot(StoryDescriptionSnapshot snapshot) {
    this.snapshot = snapshot;
  }

  public SideBySideRenderer<String> getDescriptionRenderer() {
    return descriptionRenderer;
  }

  public void setDescriptionRenderer(SideBySideRenderer<String> descriptionRenderer) {
    this.descriptionRenderer = descriptionRenderer;
  }

  public SideBySideRenderer<String> getSummaryRenderer() {
    return summaryRenderer;
  }

  public void setSummaryRenderer(SideBySideRenderer<String> summaryRenderer) {
    this.summaryRenderer = summaryRenderer;
  }

  @DefaultHandler
  public Resolution displayDiff() {
    Diff<String> summaryDiff = new Diff<String>(String.class, new String[] {snapshot.getSummary()},
        new String[] {story.getSummary()});
    summaryRenderer = new SideBySideRenderer<String>(summaryDiff);

    Diff<String> descriptionDiff = new Diff<String>(String.class, notNull(snapshot.getDescription()).split("\n"),
        notNull(story.getDescription()).split("\n"));
    descriptionRenderer = new SideBySideRenderer<String>(descriptionDiff);

    return new ForwardResolution("storyDiff.jsp");
  }

  private String notNull(String value) {
    return value == null ? "" : value;
  }

  @HandlesEvent("Rollback")
  public Resolution rollback() {
    persistenceManager.inTransaction(new PersistenceManager.UpdateHandler<Object>() {
      public Object doUpdate(PersistenceManager.UpdateReceiver receiver) {
        StoryDescriptionSnapshot snapshot = domain.loadEntity(StoryDescriptionSnapshot.class, snapshotId);

        story.setDescription(snapshot.getDescription());
        story.setSummary(snapshot.getSummary());

        receiver.update(story);
        
        return null;
      }
    });
    return new ForwardResolution("display/interstitialStoryUpdate.jsp");
  }

  @After(stages=LifecycleStage.BindingAndValidation)
  public void loadData() {
    this.snapshot = domain.loadEntity(StoryDescriptionSnapshot.class, snapshotId);
    this.story = domain.loadEntity(Story.class, storyId);
  }
}
